<?php 
class MySQLClass {
	private $_link = NULL;
	function connect($info = array()) {
		global $MysqlInfo;

		$info = count($info) > 0 ? $info : $MysqlInfo['default'];
		$this->_link = @mysql_connect($info['hostname'] , $info['username'] , $info['password']);
		if($this->_link) {
			if($this->version() > '4.1') {
				//if(!empty($this->_info['charset']))		mysql_query("SET character_set_connection=".$info['char_set'].", character_set_results=".$info['char_set'].", character_set_client=binary", $this->_link);
				mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $this->_link);
				//if($this->version() > '5.0.1')	mysql_query("SET sql_mode=''", $this->_link);
			}
			if(!mysql_select_db($info['database'], $this->_link)) die("找不到指定的数据库！");
		} else {
			exit("数据库连接失败！");
		}
	}
	function query($sql , $notice = false) {
		$query = @mysql_query($sql , $this->_link);
		if($this->error() &&  $notice) $this->halt('MySQL Query error', $sql);
		return $query;
	}
	function fetch_once($query) {
		$resultArr = array();
		if(is_string($query)) $query = $this->query($query);
		if($this->num_rows($query) > 0) $resultArr = mysql_fetch_array($query , MYSQL_ASSOC);
		return $resultArr;
	}
	function fetch_array($query) {
		$resultArr = array();
		if(is_string($query)) $query = $this->query($query);
		if($this->num_rows($query) > 0) while($res = mysql_fetch_array($query , MYSQL_ASSOC)) if($res) $resultArr[] = $res;
		return $resultArr;
	}
	function insert_id() 		{ return mysql_insert_id($this->_link); }
	function affected_rows() 	{ return mysql_affected_rows($this->_link); }
	function num_rows($query) 	{ return @mysql_num_rows($query); }

	function insert($tablename, $values, $returnid = true, $replace = false, $silent = 0) {
		$insertkeys = $insertvalues = array();
		foreach ($values as $key => $value) {
			$insertkeys[] = "`{$key}`";
			$insertvalues[] = $value === 'now' ? 'now()' : "'{$value}'";
		}
		if(count($insertkeys) < 1) return -1;
		$sql = ($replace ? 'REPLACE' : 'INSERT').' INTO `'.$tablename.'` ('.join(', ' , $insertkeys).') VALUES ('.join(", " , $insertvalues).')';
		$this->query($sql , $silent ? 'SILENT' : '');
		if($returnid && !$replace) return $this->insert_id();
	}
	function update($tablename, $setsqlarr, $wherearr = '', $silent=0) {
		$set = $where = array();
		foreach ($setsqlarr as $set_key => $set_value) $set[] = "`{$set_key}`=".($set_value === 'now' ? 'now()' : "'{$set_value}'");
		if(is_array($wherearr)) {
			foreach ($wherearr as $key => $value) $where[] = "`{$key}`='{$value}'";
			$where = ' WHERE '.join(' and ' , $where);
		} else $where = empty($wherearr) ? '' : ' WHERE '.$wherearr;

		if(count($set) < 1) return -1;
		$query = $this->query("select * from `{$tablename}` {$where}");
		$row = $this->num_rows($query);

		$sql = 'UPDATE `'.$tablename.'` SET '.join(' , ' ,$set).$where;
		$this->query($sql, $silent?'SILENT':'');
		$afr = $this->affected_rows();
		if($afr == $row) {
			return array(true , 0);
		} elseif($afr == 0) {
			if($this->error()) return array(false , 0 , $this->error());
			else return array(true , 0);
		} else {
			if($this->error()) return array(false , $row - $afr , $this->error());
			else return array(true , $row - $afr);
		}
	}

	function error()	{ return @mysql_error($this->_link); }
	function errno()	{ return intval(@mysql_errno($this->_link)); }
	function close()	{ @mysql_close($this->_link); }
	function version()	{ return mysql_get_server_info($this->_link); }
	function halt($message = '',$sql = '') { exit("MySQL Query:" . $sql ." MySQL error:" . $this->error() . " MySQL errno:" . $this->errno() . " Message:" . $message); }
	function halttest() { echo " MySQL:" . $this->error(); }
}
?>